//  import { Register } from 'register-service-worker'
// notify:通知
import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'
import { Notify } from 'vant'

//  import HomeView from '../views/HomeView.vue'
const Home=() => import('../views/home/Home.vue')
const Category=() => import('../views/category/Category.vue')
const Detail=() => import('../views/detail/Detail.vue')
const Profile=() => import('../views/profile/Profile.vue')
const Shopcar=() => import('../views/shopcar/Shopcar.vue')
const Register=()=> import('../views/profile/Register.vue')
const Login=()=> import('../views/profile/Login.vue')
const Collect=()=>import('../views/profile/Collect.vue')
const Address=()=>import('../views/profile/Address.vue')
const AddressEdit=()=>import('../views/profile/AddressEdit.vue')
const CreateOrder=()=>import('../views/order/CreateOrder.vue')
const PayTest=()=>import('../views/order/PayTest.vue')
const OrderDetail=()=>import('../views/order/OrderDetail.vue')
const Order=()=>import('../views/order/Order.vue')
const Account=()=>import('../views/profile/Account.vue')
const UpdatePwd=()=>import('../views/profile/UpdatePwd.vue')
const routes = [
  {
    path: '/',
    name: 'defaultHome',
    component: Home,
    meta:{
      //元素：显示对应标题
      title:'图书商城'
    }
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/home',
    name:'Home',
    component:Home,
    meta:{
      title:'图书商城'
    }
  },
  {
    path:'/category',
    name:'Category',
    component:Category,
    meta:{
      title:'图书商城-分类'
    }
  },
  {
    path:'/detail',
    name:'Detail',
    component:Detail,
    meta:{
      title:'图书商城-商品详细'
    }
  },
 
  {
    path:'/shopcar',
    name:'Shopcar',
    component:Shopcar,
    meta:{
      title:'图书商城-商品购物车',
      // Auth:认证  Required;要求
      isAuthRequired:true
    }
  },
  {
    path:'/profile',
    name:'Profile',
    component:Profile,
    meta:{
      title:'图书商城-个人中心',
      isAuthRequired:true
    }
  },
  {
    path:'/register',
    name:'Register',
    component:Register,
    meta:{
      title:'用户注册'
    }
  },
  {
    path:'/login',
    name:'Login',
    component:Login,
    meta:{
      title:'登入'
    }
  },
  {
    path:'/collect',
    name:'Collect',
    component:Collect,
    meta:{
      title:'图书商城-个人中心',
      isAuthRequired:true
    }
  },
  {
    path:'/address',
    name:'Address',
    component:Address,
    meta:{
      title:'地址管理',
      isAuthRequired:true
    }
  },
  {
    path:'/addressedit',
    name:'AddressEdit',
    component:AddressEdit,
    meta:{
      title:'地址编辑',
      isAuthRequired:true
    }
  },
  {
    path:'/createorder',
    name:'CreateOrder',
    component:CreateOrder,
    meta:{
      title:'創建訂單',
      isAuthRequired:true
    }
  },
  {
    path:'/paytest',
    name:'PayTest',
    component:PayTest,
    meta:{
      title:'訂單测试',
      isAuthRequired:true
    }
  },
  {
    path:'/orderdetail',
    name:'OrderDetail',
    component:OrderDetail,
    meta:{
      title:'订单详情',
      isAuthRequired:true
    }
  },
 {
  path:'/order',
  name:'Order',
  component:Order,
  meta:{
    title:'我的订单',
    isAuthRequired:true
  }
 },
 {
  path:'/setting',
  name:'Account',
  component:Account,
  meta:{
    title:'账户管理',
    isAuthRequired:true
  }
 },
 {
  path:'/updatepwd',
  name:'UpdatePwd',
  component:UpdatePwd,
  meta:{
    title:'修改密码',
    isAuthRequired:true
  }
 }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to,from,next)=>{
  //如果没有登入，在这里到login
  if(to.meta.isAuthRequired && store.state.user.isLogin==false){
    Notify({type:'success',message:'你还没有登入'})
    return next('/login')
  }else{
    next();
  }
 
  document.title=to.meta.title;
})

export default router
//meta：中继 Required:所需 auth:认证